<meta name="csrf-token" content="{{ csrf_token() }}">

<div class="modal fade" id="myModal1" role="dialog" aria-labelledby="myModalLabel" data-backdrop="static">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">New message</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close" id="remove"><span aria-hidden="true">&times;</span></button>
            </div>
            <div class="modal-body">

                <form class="form-horizontal">
                    <fieldset>
                        <div class="control-group">
                            <!-- Select Basic -->
                            <label class="control-label">班段</label>
                            <div class="controls">
                                <select class="form-control" data-live-search="true" id="trans_section">
                                    <option value="0" selected>请选择</option>
                                    <option value="1">一段班</option>
                                    <option value="2">二段班</option>
                                </select>
                            </div>
                        </div>

                        <div class="control-group">
                            <!-- Select Basic -->
                            <label class="control-label">班级</label>
                            <label style="color:red">请选择转入班级</label>
                            <div class="controls">
                                <select class="form-control select2-selection select2-selection--single" style="width:100%" id="trans_class">
                                    <option value="0" selected>请选择</option>
                                </select>
                            </div>
                        </div>

                        <div class="control-group" style="margin-top:20px">
                            <!-- Select Basic -->
                            <div class="controls" style="height:auto;background-color:#ccc;text-align:center" id="trans_info">
                            </div>
                        </div>

                        <div class="control-group">
                            <label class="control-label">转班说明</label>
                            <textarea class="form-control" id="trans_memo" name="deblock_udid" rows="8" cols="10" style="min-width: 90%"></textarea>
                        </div>

                        <input id="record_id" style="display:none">
                    </fieldset>
                </form>

            </div>
            <div class="modal-footer">
                <button type="button" id="trans_close" class="btn btn-default"  data-dismiss="modal">关闭</button>
                <button type="button" id="trans_clear" class="btn btn-warning">清空</button>
                <button type="button" id="trans_submit" class="btn btn-primary">提交申请</button>
            </div>
        </div>
    </div>
</div>

<div id="page-wrapper">
    <script type="text/javascript">
        $(function(){
            $('#trans_class').select2();
        });

        function trans(id){
            //点击时候清空弹出层
            $('#trans_class').children('option[value!=0]').remove();
            $('#trans_section option:selected').prop('selected','false');
            $('#trans_section option[value=0]').prop('selected','true');
            $('#trans_info').html('');
            $('#trans_memo').val('');
            $('#record_id').val(id);


            var row = $('input[data-id="'+id+'"]').parent().parent().parent();

            var number = row.children('td').eq(1).text();
            number = number.trim();
            var en_name = row.children('td').eq(3).text();
            en_name = en_name.trim();
            var ch_name = row.children('td').eq(2).text();
            ch_name = ch_name.trim();
            var squad = row.children('td').eq(7).text();
            squad = squad.trim();
            var type = row.children('td').eq(5).text();
            type = type.trim();


            $('#myModal1').find('.modal-title').text(squad+'-'+ch_name+'-'+en_name+'-学号：'+number+' '+type+'申请');
            $('#myModal1').modal({backdrop: 'static'});
        }

        //转班申请班级联动
        $('#trans_section').on('change',function(){
            var val = $('#trans_section option:selected').val();
            $('#trans_class').children('option[value!=0]').remove();
            if(val > 0){
                var request = {};
                request.section = val;
                $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},async:false});
                $.post('/admin/apply/get',request,function(data){
                    var html = '';
                    for(i=0;i<data.length;i++){
                        html += '<option value="'+data[i].id+'">'+data[i].name+'</option>';
                    }
                    $('#trans_class').append(html);
                });
            }
        });

        //转班申请班级联动
        $('#trans_class').on('change',function(){
            var val = $('#trans_class option:selected').val();
            if(val == 0){
                $('#trans_info').html('');
            }else if(val > 0){
                var request = {};
                request.squad = val;
                $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},async:false});
                $.post('/admin/apply/get',request,function(data){
                    $('#trans_info').html(data);
                });
            }
        });

        //转班申请清空
        $('#trans_clear').on('click',function(){
            $('#trans_class').children('option[value!=0]').remove();
            $('#trans_section option:selected').prop('selected','false');
            $('#trans_section option[value=0]').prop('selected','true');
            $('#trans_info').html('');
            $('#trans_memo').val('');
        });

        //提交转班申请
        $('#trans_submit').on('click',function(){
            if($('#trans_section').val() == 0){
                alert('请选择班段！');
                return;
            }

            if($('#trans_class').val() == 0){
                alert('请选择班级！');
                return;
            }

            var request = {squad:$('#trans_class').val()};
            request.memo = $('#trans_memo').val();
            request.id = $('#record_id').val();
            $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},async:false});
            $.post('/admin/apply/update',request,function(data){
                if(data.status < 0){
                    alert(data.message);
                    return;
                }else{
                    alert('操作成功！');
                    $('#myModal1').modal('hide');
                    window.location.reload();
                }
            });
        });
    </script>
</div>